{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Merge Triplets to Form Target Triplet"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: mergeTriplets"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #合并若干三元组以形成目标三元组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p><strong>三元组</strong> 是一个由三个整数组成的数组。给你一个二维整数数组 <code>triplets</code> ，其中 <code>triplets[i] = [a<sub>i</sub>, b<sub>i</sub>, c<sub>i</sub>]</code> 表示第 <code>i</code> 个 <strong>三元组</strong> 。同时，给你一个整数数组 <code>target = [x, y, z]</code> ，表示你想要得到的 <strong>三元组</strong> 。</p>\n",
    "\n",
    "<p>为了得到 <code>target</code> ，你需要对 <code>triplets</code> 执行下面的操作 <strong>任意次</strong>（可能 <strong>零</strong> 次）：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>选出两个下标（下标 <strong>从 0 开始</strong> 计数）<code>i</code> 和 <code>j</code>（<code>i != j</code>），并 <strong>更新</strong> <code>triplets[j]</code> 为 <code>[max(a<sub>i</sub>, a<sub>j</sub>), max(b<sub>i</sub>, b<sub>j</sub>), max(c<sub>i</sub>, c<sub>j</sub>)]</code> 。\n",
    "\n",
    "\t<ul>\n",
    "\t\t<li>例如，<code>triplets[i] = [2, 5, 3]</code> 且 <code>triplets[j] = [1, 7, 5]</code>，<code>triplets[j]</code> 将会更新为 <code>[max(2, 1), max(5, 7), max(3, 5)] = [2, 7, 5]</code> 。</li>\n",
    "\t</ul>\n",
    "\t</li>\n",
    "</ul>\n",
    "\n",
    "<p>如果通过以上操作我们可以使得目标 <strong>三元组</strong> <code>target</code> 成为 <code>triplets</code> 的一个 <strong>元素</strong> ，则返回 <code>true</code> ；否则，返回 <code>false</code> 。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>triplets = [[2,5,3],[1,8,4],[1,7,5]], target = [2,7,5]\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>执行下述操作：\n",
    "- 选择第一个和最后一个三元组 [<strong>[2,5,3]</strong>,[1,8,4],<strong>[1,7,5]</strong>] 。更新最后一个三元组为 [max(2,1), max(5,7), max(3,5)] = [2,7,5] 。triplets = [[2,5,3],[1,8,4],<strong>[2,7,5]</strong>]\n",
    "目标三元组 [2,7,5] 现在是 triplets 的一个元素。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>triplets = [[1,3,4],[2,5,8]], target = [2,5,8]\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>目标三元组 [2,5,8] 已经是 triplets 的一个元素。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>triplets = [[2,5,3],[2,3,4],[1,2,5],[5,2,3]], target = [5,5,5]\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>执行下述操作：\n",
    "- 选择第一个和第三个三元组 [<strong>[2,5,3]</strong>,[2,3,4],<strong>[1,2,5]</strong>,[5,2,3]] 。更新第三个三元组为 [max(2,1), max(5,2), max(3,5)] = [2,5,5] 。triplets = [[2,5,3],[2,3,4],<strong>[2,5,5]</strong>,[5,2,3]] 。\n",
    "- 选择第三个和第四个三元组 [[2,5,3],[2,3,4],<strong>[2,5,5]</strong>,<strong>[5,2,3]</strong>] 。更新第四个三元组为 [max(2,5), max(5,2), max(5,3)] = [5,5,5] 。triplets = [[2,5,3],[2,3,4],[2,5,5],<strong>[5,5,5]</strong>] 。\n",
    "目标三元组 [5,5,5] 现在是 triplets 的一个元素。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>triplets = [[3,4,5],[4,5,6]], target = [3,2,5]\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：</strong>无法得到 [3,2,5] ，因为 triplets 不含 2 。\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= triplets.length <= 10<sup>5</sup></code></li>\n",
    "\t<li><code>triplets[i].length == target.length == 3</code></li>\n",
    "\t<li><code>1 <= a<sub>i</sub>, b<sub>i</sub>, c<sub>i</sub>, x, y, z <= 1000</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [merge-triplets-to-form-target-triplet](https://leetcode.cn/problems/merge-triplets-to-form-target-triplet/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [merge-triplets-to-form-target-triplet](https://leetcode.cn/problems/merge-triplets-to-form-target-triplet/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[2,5,3],[1,8,4],[1,7,5]]\\n[2,7,5]', '[[3,4,5],[4,5,6]]\\n[3,2,5]', '[[2,5,3],[2,3,4],[1,2,5],[5,2,3]]\\n[5,5,5]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        # given [a1,b1,c1] and [a2,b2,c2], and target is [a,b,c]\n",
    "        # if a1 = a, and b2 = b\n",
    "        # we can only successfully merge if b1 <= b, c1 <= c, and a2 <= a, c2 <= c\n",
    "        # if other words, in a triplet, other than the matching element\n",
    "        # all other elements need to be less than or equal to the target\n",
    "\n",
    "        # Implementation wise, we can have an list of length 3 that will give us the final answer\n",
    "        \n",
    "        a, b, c = target\n",
    "        l = [False] * 3\n",
    "        for a1, b1, c1 in triplets:\n",
    "            if a1 <= a and b1 <= b and c1 <= c:\n",
    "                if a1 == a:\n",
    "                    l[0] = True\n",
    "                if b1 == b:\n",
    "                    l[1] = True\n",
    "                if c1 == c:\n",
    "                    l[2] = True\n",
    "            # pre terminate when target can be obtained\n",
    "            if l[0] and l[1] and l[2]:\n",
    "                return True\n",
    "        \n",
    "        return l[0] and l[1] and l[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        x, y, z = target\n",
    "        a, b, c = 0, 0, 0\n",
    "\n",
    "        for ai, bi, ci, in triplets:\n",
    "            if ai <= x and bi <= y and ci <= z:\n",
    "                a, b, c = max(a, ai), max(b, bi), max(c, ci)\n",
    "\n",
    "        return (a, b, c) == (x, y, z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        x,y,z = target\n",
    "        a,b,c = 0,0,0\n",
    "        for ai,bi,ci in triplets:\n",
    "            if ai <= x and bi <= y and ci <= z:\n",
    "                a,b,c = max(a,ai),max(b,bi),max(c,ci)\n",
    "        return (a,b,c) == (x,y,z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        x, y, z = target\n",
    "        a, b, c = 0, 0, 0\n",
    "\n",
    "        for u, v, w in triplets:\n",
    "            if u <= x and v <= y and w <= z:\n",
    "                a, b, c = max(a, u), max(b, v), max(c, w)\n",
    "\n",
    "        return (a, b, c) == (x, y, z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        a = b = c = False\n",
    "        for x, y, z in triplets:\n",
    "            if not a and x == target[0] and y <= target[1] and z <= target[2]:\n",
    "                a = True\n",
    "            if not b and y == target[1] and x <= target[0] and z <= target[2]:\n",
    "                b = True\n",
    "            if not c and z == target[2] and x <= target[0] and y <= target[1]:\n",
    "                c = True\n",
    "            if a and b and c:\n",
    "                return True\n",
    "        return False\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        # # 执行操作只会让数变大/不变，所以应该选择满足all(triplet[i]<=target[i] for i in range(3))的三元组进行合并\n",
    "        # if not all(t <= tg for t,tg in zip(triplets[0], target)):\n",
    "        #     triplets[0] = [0,0,0]\n",
    "        # for ai, bi, ci in triplets:\n",
    "        #     if ai<=target[0] and bi<=target[1] and ci<=target[2]:\n",
    "        #         triplets[0][0], triplets[0][1], triplets[0][2] = max(triplets[0][0],ai), max(triplets[0][1],bi), max(triplets[0][2],ci)\n",
    "        # return triplets[0]==target\n",
    "\n",
    "        # 只要对于满足all(triplet[i]<=target[i] for i in range(3))的三元组，各部分都出现了x,y,z，则能够更新为xyz\n",
    "        found = [False] * 3\n",
    "        for ai,bi,ci in triplets:\n",
    "            if ai<=target[0] and bi<=target[1] and ci<=target[2]:\n",
    "                found[0] = found[0] or ai == target[0]\n",
    "                found[1] = found[1] or bi == target[1]\n",
    "                found[2] = found[2] or ci == target[2]\n",
    "        return all(found)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        x, y, z = target\n",
    "        a, b, c = 0, 0, 0\n",
    "\n",
    "        for u, v, w in triplets:\n",
    "            if u > x or v > y or w > z: continue\n",
    "            a, b, c = max(a, u), max(b, v), max(c, w)\n",
    "            if (a, b, c) == (x, y, z): return True\n",
    "\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\r\n",
    "        # for n in range(len(triplets)):\r\n",
    "            # if triplets[n].count\r\n",
    "        # for n in target:\r\n",
    "        #     if triplets.count(n)==0:\r\n",
    "        #         return False\r\n",
    "        x,y,z = target\r\n",
    "        x_ = y_ = z_ = False\r\n",
    "        for a,b,c in list(triplets):\r\n",
    "            if a > x or b > y or c > z:\r\n",
    "                continue\r\n",
    "            if a == x:\r\n",
    "                x_ = True\r\n",
    "            if b == y:\r\n",
    "                y_ = True\r\n",
    "            if c == z:\r\n",
    "                z_ = True\r\n",
    "        return x_ and y_ and z_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        found = [0, 0, 0]\n",
    "        for t in triplets:\n",
    "            state = [-1 if i[0] < i[1] else (0 if i[0] == i[1] else 1) for i in zip(t, target)]\n",
    "            if 1 in state:\n",
    "                continue\n",
    "            for i in range(3):\n",
    "                found[i] = 1 if found[i] > 0 or state[i] == 0 else 0\n",
    "            if found == [1, 1, 1]:\n",
    "                return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        a, b, c = target\n",
    "\n",
    "        x = y = z = 0\n",
    "        for i, j, k in triplets:\n",
    "            if i<=a and j<=b and k<=c:\n",
    "                x, y, z = max(x, i), max(y, j), max(z, k)\n",
    "        \n",
    "        return (a, b, c) == (x, y, z)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        flag1 = flag2 = flag3 = False;\n",
    "\n",
    "        for x, y, z in triplets:\n",
    "            if x == target[0] and y <= target[1] and z <= target[2]:\n",
    "                flag1 = True\n",
    "            if x <= target[0] and y == target[1] and z <= target[2]:\n",
    "                flag2 = True\n",
    "            if x <= target[0] and y <= target[1] and z == target[2]:\n",
    "                flag3 = True\n",
    "        \n",
    "        return flag1 and flag2 and flag3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        found = [False] * 3\n",
    "        for t in triplets:\n",
    "            if t[0] <= target[0] and t[1] <= target[1] and t[2] <= target[2]:\n",
    "                found[0] = found[0] or t[0] == target[0]\n",
    "                found[1] = found[1] or t[1] == target[1]\n",
    "                found[2] = found[2] or t[2] == target[2]\n",
    "        return all(found)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        candidates = []\n",
    "        idx = [False, False, False]\n",
    "\n",
    "        for triplet in triplets:\n",
    "            if all(triplet[i] <= target[i] for i in range(3)):\n",
    "                candidates.append(triplet)\n",
    "                \n",
    "                for i in range(3):\n",
    "                    if triplet[i] == target[i]:\n",
    "                        idx[i] = True\n",
    "\n",
    "        return len(candidates) > 0 and all(idx)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        check = [False] * 3\n",
    "        i = 0\n",
    "        while i < len(triplets) and not (check[0] and check[1] and check[2]):\n",
    "            t = triplets[i]\n",
    "            if target[0] >= t[0] and target[1] >= t[1] and target[2] >= t[2]:\n",
    "                if target[0] == t[0]:\n",
    "                    check[0] = True\n",
    "                if target[1] == t[1]:\n",
    "                    check[1] = True\n",
    "                if target[2] == t[2]:\n",
    "                    check[2] = True\n",
    "            i += 1\n",
    "        \n",
    "        return check[0] and check[1] and check[2] \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def max_(a, b):\n",
    "    return a if a > b else b\n",
    "\n",
    "\n",
    "def min_(a, b):\n",
    "    return a if a < b else b\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        a, b, c = target[:]\n",
    "        x = y = z = 0\n",
    "        for x0, y0, z0 in triplets:\n",
    "            if x0<=a and y0<=b and z0<=c:\n",
    "                x = max_(x, x0)\n",
    "                y = max_(y, y0)\n",
    "                z = max_(z, z0)\n",
    "        return x == a and y == b and z == c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        # 只要3个数都小于等于目标，就可以加入\n",
    "        cur = [0] * 3\n",
    "        for a, b, c in triplets:\n",
    "            if a <= target[0] and b <= target[1] and c <= target[2]:\n",
    "                cur[0] = max(cur[0], a)\n",
    "                cur[1] = max(cur[1], b)\n",
    "                cur[2] = max(cur[2], c)\n",
    "                if cur == target:\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        a = b = c = -1\n",
    "        for x, y, z in triplets:\n",
    "            if x <= target[0] and y <= target[1] and z <= target[2]:\n",
    "                a = max(a, x)\n",
    "                b = max(b, y)\n",
    "                c = max(c, z)\n",
    "        if target[0] == a and target[1] == b and target[2] == c:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        for i in range(3):\n",
    "            s = {0, 1, 2} - {i}\n",
    "            for tr in triplets:\n",
    "                if tr[i] != target[i]:\n",
    "                    continue\n",
    "                for j in s:\n",
    "                    if tr[j] > target[j]:\n",
    "                        break\n",
    "                else:\n",
    "                    break\n",
    "            else:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        if target in triplets:\n",
    "            return True\n",
    "        else:\n",
    "            x,y,z=target\n",
    "            x_=y_=z_=False\n",
    "            for a,b,c in list(triplets):\n",
    "                if a>x or b>y or c>z:\n",
    "                    continue\n",
    "                if a==x:\n",
    "                    x_=True\n",
    "                if b==y:\n",
    "                    y_=True\n",
    "                if c==z:\n",
    "                    z_=True\n",
    "        return x_ and y_ and z_\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        lookup = [False, False, False]\n",
    "        for triplet in triplets:\n",
    "            cur_lookup = [False, False, False]\n",
    "            addable = True\n",
    "            for i in range(3):\n",
    "                if triplet[i] > target[i]:\n",
    "                    addable = False\n",
    "                    break\n",
    "                elif triplet[i] == target[i]:\n",
    "                    cur_lookup[i] = True\n",
    "            if addable:\n",
    "                for i in range(len(cur_lookup)):\n",
    "                    if cur_lookup[i]:\n",
    "                        lookup[i] = cur_lookup[i]\n",
    "        for val in lookup:\n",
    "            if not val:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        cur_max = [0,0,0]\n",
    "        for a,b,c in triplets:\n",
    "            if a > target[0] or b > target[1] or c > target[2]:\n",
    "                continue\n",
    "            else:\n",
    "                cur_max[0] = max(cur_max[0], a)\n",
    "                cur_max[1] = max(cur_max[1], b)\n",
    "                cur_max[2] = max(cur_max[2], c)\n",
    "        return cur_max == target\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        x,y,z = target\n",
    "        x_ = y_ = z_ = False\n",
    "        for a,b,c in list(triplets):\n",
    "            if a > x or b > y or c > z:\n",
    "                continue\n",
    "            if a == x:\n",
    "                x_ = True\n",
    "            if b == y:\n",
    "                y_ = True\n",
    "            if c == z:\n",
    "                z_ = True\n",
    "        return x_ and y_ and z_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        if len(triplets) == 1:\n",
    "            return target == triplets[0]\n",
    "        res = [-float(\"inf\"), -float(\"inf\"), -float(\"inf\")]\n",
    "        for tri in triplets:\n",
    "            if tri[0] > target[0] or tri[1] > target[1] or tri[2] > target[2]:\n",
    "                print(tri, \"no\")\n",
    "                continue\n",
    "            else:\n",
    "                res = [max(tri[0], res[0]), max(tri[1], res[1]), max(tri[2], res[2])]\n",
    "                print(res, \"new\")\n",
    "        return res == target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        x,y,z = target\n",
    "        x_ = y_ = z_ = False\n",
    "        for a,b,c in list(triplets):\n",
    "            if a > x or b > y or c > z:\n",
    "                continue\n",
    "            if a == x:\n",
    "                x_ = True\n",
    "            if b == y:\n",
    "                y_ = True\n",
    "            if c == z:\n",
    "                z_ = True\n",
    "        return x_ and y_ and z_\n",
    "\n",
    "\n",
    "        # queue = list()\n",
    "        # for x,y,z in triplets:\n",
    "        #     #他们中是否有等于三位数中一个的\n",
    "        #     if x!=target[0] and y!=target[1] and z!=target[2]:\n",
    "        #         continue\n",
    "        #     #其他不等的是否不大于目标数\n",
    "        #     if x>target[0] or y>target[1] or z>target[2]:\n",
    "        #         continue\n",
    "        #     queue.append([x,y,z])\n",
    "        # if len(queue) == 0:\n",
    "        #     return False\n",
    "        # tracker = [False]*3\n",
    "        # x,y,z = queue[0]\n",
    "        # for i in range(1,len(queue)):\n",
    "        #     w,h,l = queue[i]\n",
    "        #     x = max(x,w)\n",
    "        #     y = max(y,h)\n",
    "        #     z = max(z,l)\n",
    "        \n",
    "        # if [x,y,z] != target:\n",
    "        #     return False\n",
    "        # return True\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "\n",
    "        is_find = [False for _ in range(3)]\n",
    "\n",
    "        for (a,b,c) in triplets:\n",
    "            if (a == target[0] or b == target[1] or c == target[2]) and a <= target[0] and b <= target[1] and c <= target[2]:\n",
    "                if a == target[0]:\n",
    "                    is_find[0] = True\n",
    "                if b == target[1]:\n",
    "                    is_find[1] = True\n",
    "                if c == target[2]:\n",
    "                    is_find[2] = True\n",
    "        \n",
    "        if is_find[0] and is_find[1] and is_find[2]:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        s = {0,1,2}\n",
    "        for x, y, z in triplets:\n",
    "            if x > target[0] or y > target[1] or z > target[2]:\n",
    "                continue\n",
    "            if x == target[0] and 0 in s:\n",
    "                s.remove(0)\n",
    "            if y == target[1] and 1 in s:\n",
    "                s.remove(1)\n",
    "            if z == target[2] and 2 in s:\n",
    "                s.remove(2)\n",
    "        return len(s) == 0 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        max_a = 0\n",
    "        max_b = 0\n",
    "        max_c = 0\n",
    "        for i in range(0, len(triplets)):\n",
    "            row = triplets[i]\n",
    "            a = row[0]\n",
    "            b = row[1]\n",
    "            c = row[2]\n",
    "            if a <= target[0] and b <= target[1] and c <= target[2]:\n",
    "                max_a = max(a, max_a)\n",
    "                max_b = max(b, max_b)\n",
    "                max_c = max(c, max_c)\n",
    "        return max_a == target[0] and max_b == target[1] and max_c == target[2]\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        a,b,c = 0,0,0\n",
    "        for triplet in triplets:\n",
    "            if triplet[0]<=target[0] and triplet[1]<=target[1] and triplet[2]<=target[2]:\n",
    "                a=max(a,triplet[0])\n",
    "                b=max(b,triplet[1])\n",
    "                c=max(c,triplet[2])\n",
    "\n",
    "        return a==target[0] and b==target[1] and c==target[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        ans = [0]*3\n",
    "        for a,b,c in triplets:\n",
    "            if a <= target[0] and b <= target[1] and c <= target[2]:\n",
    "                if a == target[0]:\n",
    "                    ans[0] = 1\n",
    "                if b == target[1]:\n",
    "                    ans[1] = 1\n",
    "                if c == target[2]:\n",
    "                    ans[2] = 1\n",
    "        return sum(ans) == 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        flag1 = flag2 = flag3 = False\n",
    "        for t in triplets:\n",
    "            if t[0] == target[0] and t[1] <= target[1] and t[2] <= target[2]:\n",
    "                flag1 = True\n",
    "            if t[1] == target[1] and t[0] <= target[0] and t[2] <= target[2]:\n",
    "                flag2 = True\n",
    "            if t[2] == target[2] and t[0] <= target[0] and t[1] <= target[1]:\n",
    "                flag3 = True\n",
    "        return flag1 and flag2 and flag3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        t = [0,0,0]\n",
    "        for i,j,k in triplets:\n",
    "            if i<=target[0] and j<= target[1] and k<= target[2]:\n",
    "                t[0] = max(t[0],i)\n",
    "                t[1] = max(t[1],j)\n",
    "                t[2] = max(t[2],k)\n",
    "        return t==target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        check = [False] * 3\n",
    "        i = 0\n",
    "        while i < len(triplets) and not (check[0] and check[1] and check[2]):\n",
    "            t = triplets[i]\n",
    "            if target[0] >= t[0] and target[1] >= t[1] and target[2] >= t[2]:\n",
    "              for index, value in enumerate(t):\n",
    "                if target[index] == t[index]:\n",
    "                  check[index] = True\n",
    "            i += 1\n",
    "        \n",
    "        return check[0] and check[1] and check[2] \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        cnt = defaultdict(list)\n",
    "        for t in triplets:\n",
    "            if t == target:\n",
    "                return True\n",
    "            if t[0] == target[0]:\n",
    "                cnt[0].append(t)\n",
    "            if t[1] == target[1]:\n",
    "                cnt[1].append(t)\n",
    "            if t[2] == target[2]:\n",
    "                cnt[2].append(t) \n",
    "        ret = 0\n",
    "        for v in cnt.values():\n",
    "            for t in v:\n",
    "                if all(x <= y for x, y in zip(t, target)):\n",
    "                    ret += 1\n",
    "                    break\n",
    "        return ret >= 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        cnt = defaultdict(list)\n",
    "        for t in triplets:\n",
    "            if t == target:\n",
    "                return True\n",
    "            if all(x <= y for x, y in zip(t, target)):\n",
    "                if t[0] == target[0]:\n",
    "                    cnt[0].append(t)\n",
    "                if t[1] == target[1]:\n",
    "                    cnt[1].append(t)\n",
    "                if t[2] == target[2]:\n",
    "                    cnt[2].append(t) \n",
    "        return len(cnt) == 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        flag1 = False\n",
    "        flag2 = False\n",
    "        flag3 = False\n",
    "        for t in triplets:\n",
    "            if t[0] > target[0] or t[1] > target[1] or t[2] > target[2]:\n",
    "                continue\n",
    "            else:\n",
    "                if t[0] == target[0]:\n",
    "                    flag1 = True\n",
    "                if t[1] == target[1]:\n",
    "                    flag2 = True\n",
    "                if t[2] == target[2]:\n",
    "                    flag3 = True \n",
    "        return flag1 and flag2 and flag3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], t: List[int]) -> bool:\n",
    "\n",
    "        i = 0\n",
    "        for x, y, z in triplets:\n",
    "            if x <= t[0] and y <= t[1] and z <= t[2] and (x == t[0] or y == t[1] or z == t[2]):\n",
    "                if x == t[0]:\n",
    "                    i |= 1\n",
    "                if y == t[1]:\n",
    "                    i |= 2\n",
    "                if z == t[2]:\n",
    "                    i |= 4\n",
    "                if i == 7:\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        n = len(triplets)\n",
    "        res = [-inf, -inf, -inf]\n",
    "        for i in range(n):\n",
    "            cur = triplets[i]\n",
    "            if cur[0] > target[0] or cur[1] > target[1] or cur[2] > target[2]:\n",
    "                continue \n",
    "            res[0] = max(res[0], cur[0])\n",
    "            res[1] = max(res[1], cur[1])\n",
    "            res[2] = max(res[2], cur[2])\n",
    "        return res == target\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        cnt = defaultdict(list)\n",
    "        n = len(triplets)\n",
    "        for t in triplets:\n",
    "            if t == target:\n",
    "                return True\n",
    "            if t[0] == target[0]:\n",
    "                cnt[0].append(t)\n",
    "            if t[1] == target[1]:\n",
    "                cnt[1].append(t)\n",
    "            if t[2] == target[2]:\n",
    "                cnt[2].append(t) \n",
    "        if len(cnt) < 3: return False\n",
    "        print(cnt)\n",
    "        ret = 0\n",
    "        for v in cnt.values():\n",
    "            for t in v:\n",
    "                if all(x <= y for x, y in zip(t, target)):\n",
    "                    ret += 1\n",
    "                    break\n",
    "        return ret >= 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        a = [0]*3\n",
    "        for l in triplets:\n",
    "            if l[0] == target[0] and l[1] <= target[1] and l[2] <= target[2]:\n",
    "                a[0] = 1\n",
    "            if l[0] <= target[0] and l[1] == target[1] and l[2] <= target[2]:\n",
    "                a[1] = 1\n",
    "            if l[0] <= target[0] and l[1] <= target[1] and l[2] == target[2]:\n",
    "                a[2] = 1\n",
    "            if a == [1,1,1]:\n",
    "                return True\n",
    "        return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        filtered_list = []\n",
    "        for elem in triplets:\n",
    "            if elem[0] == target[0] or elem[1] == target[1] or elem[2] == target[2]:\n",
    "                if elem[0] <= target[0] and elem[1] <= target[1] and elem[2] <= target[2]:\n",
    "                    filtered_list.append(elem)\n",
    "        flag = [False, False, False]\n",
    "        for elem in filtered_list:\n",
    "            if elem[0] == target[0]:\n",
    "                flag[0] = True\n",
    "            if elem[1] == target[1]:\n",
    "                flag[1] = True\n",
    "            if elem[2] == target[2]:\n",
    "                flag[2] = True\n",
    "        return flag[0] and flag[1] and flag[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        # 执行操作只会让数变大/不变，所以应该选择满足all(triplet[i]<=target[i] for i in range(3))的三元组进行合并\n",
    "        if not all(t <= tg for t,tg in zip(triplets[0], target)):\n",
    "            triplets[0] = [0,0,0]\n",
    "        for ai, bi, ci in triplets:\n",
    "            if ai<=target[0] and bi<=target[1] and ci<=target[2]:\n",
    "                triplets[0][0], triplets[0][1], triplets[0][2] = max(triplets[0][0],ai), max(triplets[0][1],bi), max(triplets[0][2],ci)\n",
    "        return triplets[0]==target\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        x,y,z = target\n",
    "        x_ = y_ = z_ = False\n",
    "        for a,b,c in list(triplets):\n",
    "            if a > x or b > y or c > z:\n",
    "                continue\n",
    "            if a == x:\n",
    "                x_ = True\n",
    "            if b == y:\n",
    "                y_ = True\n",
    "            if c == z:\n",
    "                z_ = True\n",
    "        return x_ and y_ and z_\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        if target in triplets:\n",
    "            return True\n",
    "        else:\n",
    "            x,y,z=target\n",
    "            x_=y_=z_=False\n",
    "            for a,b,c in list(triplets):\n",
    "                if a>x or b>y or c>z:\n",
    "                    continue\n",
    "                if a==x:\n",
    "                    x_=True\n",
    "                if b==y:\n",
    "                    y_=True\n",
    "                if c==z:\n",
    "                    z_=True\n",
    "        return x_ and y_ and z_\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        if target in triplets:\n",
    "            return True\n",
    "        \n",
    "        first = 0\n",
    "        second = 0\n",
    "        third = 0\n",
    "        for t in triplets:\n",
    "            if t[0] == target[0] and t[1] <= target[1] and t[2] <= target[2]:\n",
    "                first = t\n",
    "            if t[1] == target[1] and t[0] <= target[0] and t[2] <= target[2]:\n",
    "                second = t\n",
    "            if t[2] == target[2] and t[1] <= target[1] and t[0] <= target[0]:\n",
    "                third = t\n",
    "        if first == 0 or second == 0 or third == 0:\n",
    "            return False\n",
    "        result = [max(first[0],second[0],third[0]),max(first[1],second[1],third[1]),max(first[2],second[2],third[2])]\n",
    "        print(first,second,third)\n",
    "        return result == target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        x,y,z=target\n",
    "        a,b,c=0,0,0\n",
    "\n",
    "        for ai,bi,ci in triplets:\n",
    "            if ai<=x and bi<=y and ci<=z:\n",
    "                a=max(a,ai)\n",
    "                b=max(b,bi)\n",
    "                c=max(c,ci)\n",
    "        \n",
    "        return (a,b,c)==(x,y,z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        # Initialize three variables to keep track of the maximum values for each element in the triplets\n",
    "        max_a, max_b, max_c = 0, 0, 0\n",
    "        \n",
    "        # Iterate through each triplet in the list\n",
    "        for triplet in triplets:\n",
    "            # Check if the current triplet can contribute to the maximum values\n",
    "            if triplet[0] <= target[0] and triplet[1] <= target[1] and triplet[2] <= target[2]:\n",
    "                max_a = max(max_a, triplet[0])\n",
    "                max_b = max(max_b, triplet[1])\n",
    "                max_c = max(max_c, triplet[2])\n",
    "        \n",
    "        # Check if the maximum values match the target triplet\n",
    "        return max_a == target[0] and max_b == target[1] and max_c == target[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        if len(triplets) == 1:\n",
    "            return target == triplets[0]\n",
    "        res = [-float(\"inf\"), -float(\"inf\"), -float(\"inf\")]\n",
    "        for tri in triplets:\n",
    "            if tri[0] > target[0] or tri[1] > target[1] or tri[2] > target[2]:\n",
    "                print(tri, \"no\")\n",
    "                continue\n",
    "            else:\n",
    "                res = [max(tri[0], res[0]), max(tri[1], res[1]), max(tri[2], res[2])]\n",
    "                print(res, \"new\")\n",
    "        return res == target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        judge = [0] * 3\n",
    "        for i in range(0,len(triplets)):\n",
    "            k = triplets[i]\n",
    "            if k[0] == target[0] and k[1] <= target[1] and k[2] <= target[2]:\n",
    "                judge[0] = 1\n",
    "            if k[1] == target[1] and k[2] <= target[2] and k[0] <= target[0]:\n",
    "                judge[1] = 1\n",
    "            if k[2] == target[2] and k[1] <= target[1] and k[0] <= target[0]:\n",
    "                judge[2] = 1\n",
    "        return sum(judge) == 3\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        s1 = set()\n",
    "        s2 = set()\n",
    "        s3 = set()\n",
    "        for i,(x, y, z) in enumerate(triplets):\n",
    "            if x == target[0] and y <= target[1] and z <= target[2]:\n",
    "                s1.add(i)\n",
    "            if y == target[1] and x <= target[0] and z <= target[2]:\n",
    "                s2.add(i)\n",
    "            if z == target[2] and x <= target[0] and y <= target[1]:\n",
    "                s3.add(i)\n",
    "        return bool(len(s1) and len(s2) and len(s3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        x, y, z = target\n",
    "        a, b, c = 0, 0, 0\n",
    "        \n",
    "        for ai, bi, ci in triplets:\n",
    "            if ai <= x and bi <= y and ci <= z:\n",
    "                a, b, c = max(a, ai), max(b, bi), max(c, ci)\n",
    "        \n",
    "        return (a, b, c) == (x, y, z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        x, y, z = 0, 0, 0\n",
    "        for t in triplets:\n",
    "            if t[0] > target[0] or t[1] > target[1] or t[2] > target[2]:\n",
    "                continue\n",
    "            x = max(t[0], x)\n",
    "            y = max(t[1],y)\n",
    "            z = max(t[2],z)\n",
    "        return x == target[0] and y == target[1] and z == target[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:\n",
    "        x,y,z=target\n",
    "        a,b,c=0,0,0\n",
    "\n",
    "        for ai,bi,ci in triplets:\n",
    "            if ai<=x and bi<=y and ci<=z:\n",
    "                a=max(a,ai)\n",
    "                b=max(b,bi)\n",
    "                c=max(c,ci)\n",
    "        \n",
    "        return (a,b,c)==(x,y,z)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
